# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=zfs
pkgver=2.1.5
pkgrel=0
pkgdesc="Advanced filesystem and volume manager"
url="https://openzfs.org"
arch="all"
license="CDDL-1.0"
provides="spl=$pkgver-r$pkgrel" # Mitigate upgrade conflicts
depends_dev="attr-dev e2fsprogs-dev glib-dev libtirpc-dev openssl1.1-compat-dev util-linux-dev"
makedepends="$depends_dev automake autoconf libtool linux-headers python3-dev py3-cffi py3-setuptools"
options="!check" # need to be run on live system w/ ZFS loaded.
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc $pkgname-libs $pkgname-dracut::noarch
	$pkgname-udev $pkgname-scripts $pkgname-utils-py:utils_py:noarch py3-pyzfs:pyzfs:noarch"
source="https://github.com/openzfs/zfs/releases/download/zfs-$pkgver/zfs-$pkgver.tar.gz
	dont-fsck-or-mount-umount-fstab-entries.patch
	"

prepare() {
	default_prepare
	autoreconf -vif
}

build() {
	export LIBS="$LIBS -lintl"
	./configure --prefix=/usr \
		--with-tirpc \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-config=user \
		--with-udevdir=/lib/udev \
		--disable-systemd \
		--disable-static \
		--with-python=3 \
		--enable-pyzfs
	make
}

package() {
	make DESTDIR="$pkgdir" install
	rm -rf "$pkgdir"/usr/share/initramfs-tools

	# Fix permissions of zfs OpenRC service configuration (#13840)
	chmod 644 "$pkgdir"/etc/conf.d/zfs
}

dracut() {
	pkgdesc="$pkgdesc (dracut)"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/dracut "$subpkgdir"/usr/lib
}

udev() {
	pkgdesc="$pkgdesc (udev)"
	mkdir -p "$subpkgdir"/lib
	mv "$pkgdir"/lib/udev "$subpkgdir"/lib
}

scripts() {
	pkgdesc="$pkgdesc (scripts)"
	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/usr/share/zfs "$subpkgdir"/usr/share
}

utils_py() {
	pkgdesc="$pkgdesc (python utils)"
	depends="python3"
	mkdir -p "$subpkgdir"/usr/bin
	local file
	for file in arc_summary arcstat dbufstat; do
		mv "$pkgdir"/usr/bin/$file "$subpkgdir"/usr/bin
	done
}

pyzfs() {
	pkgdesc="$pkgdesc (Python lib to interact with ZFS)"
	depends="python3 $pkgname"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/python* "$subpkgdir"/usr/lib
}

sha512sums="
d9ccf1049cefa9167d25f71fbdca70092cd02368b60f09341e6489fb68dc5f89e87b026b0191f4d81181a8851449124d824a1d959d0e2fb29c8a3d624edc4f03  zfs-2.1.5.tar.gz
3c2ffea6a6ea05a9953b965989198801d3528f281f9099401836fbdcdbae4f439ae65e3e68096ca7d9de7c39681aca3aa9cd71885fe14cfe17539fdd744a9925  dont-fsck-or-mount-umount-fstab-entries.patch
"
